home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / usbcreator / wrap_label.py < prev   
Encoding:
Python Source  |  2009-04-17  |  1.8 KB  |  57 lines

  1. import gobject
  2. import pygtk
  3. import gtk
  4. import pango
  5.  
  6. class WrapLabel(gtk.Label):
  7.     __gtype_name__ = 'WrapLabel'
  8.     def __init__(self, text = ''):
  9.         gtk.Label.__init__(self)
  10.         self.m_wrap_width = 0
  11.  
  12.         self.get_layout().set_wrap(pango.WRAP_WORD_CHAR)
  13.         self.set_alignment(0.0, 0.0)
  14.         self.set_markup(text)
  15.         self.connect('size-request', self.on_size_request)
  16.         self.connect('size-allocate', self.on_size_allocate)
  17.  
  18.     def set_text(self, text):
  19.         gtk.Label.set_text(self, text)
  20.         self.set_wrap_width(self.m_wrap_width)
  21.  
  22.     def set_markup(self, text):
  23.         gtk.Label.set_markup(self, text)
  24.         self.set_wrap_width(self.m_wrap_width)
  25.  
  26.     def on_size_request(self, widget, requisition):
  27.         width, height = self.get_layout().get_pixel_size()
  28.         requisition.width = 0
  29.         requisition.height = height
  30.  
  31.     def on_size_allocate(self, widget, allocation):
  32.         gtk.Label.size_allocate(self, allocation)
  33.         self.set_wrap_width(allocation.width)
  34.  
  35.     def set_wrap_width(self, width):
  36.         if width == 0:
  37.             return
  38.  
  39.         self.get_layout().set_width(width * pango.SCALE)
  40.         if self.m_wrap_width != width:
  41.             self.m_wrap_width = width
  42.             self.queue_resize()
  43.  
  44. gobject.type_register(WrapLabel)
  45.         
  46. if __name__ == '__main__':
  47.     w = gtk.Window(gtk.WINDOW_TOPLEVEL)
  48.     l = WrapLabel("This is a very long label that should span many lines. "
  49.         "It's a good example of what the WrapLabel can do, and "
  50.         "includes formatting, like <b>bold</b>, <i>italic</i>, "
  51.         "and <u>underline</u>. The window can be wrapped to any "
  52.         "width, unlike the standard Gtk::Label, which is set to "
  53.         "a certain wrap width.")
  54.     w.add(l)
  55.     w.show_all()
  56.     gtk.main()
  57.